﻿using System;
using System.Collections.Generic;
using System.Web;

using Gift.DAL;
using Gift.Model;
using Gift.Utile;

namespace WebSite
{
    public class BasePage : System.Web.UI.Page
    {

        readonly DALProfessionAreaType theProfessionAreaType = new DALProfessionAreaType();
        tbProfessionAreaType oProfessionAreaType;
        public string ClientIP
        {
            get
            {
                string userIP;
                if(Request.ServerVariables["HTTP_VIA"] == null)
               {
                  userIP= Request.UserHostAddress;
                 }
               else
               {
                   userIP= Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                }
                 return userIP;

            }
        }

        public Dictionary<int, decimal> DicProductPrice
        {
            get {
                if (Session["DicProductPrice"] == null)
                {
                    return null;
                }
                else
                {
                    return Session["DicProductPrice"] as Dictionary<int, decimal>;
                }
            }
            set { Session["DicProductPrice"] = value; }
        }

        public decimal ShopProductPrice
        {
            get
            {
                if (Session["OtherProductPrice"] == null)
                {
                    return 0;
                }
                else
                {
                    return decimal.Parse(Session["OtherProductPrice"].ToString());
                }
            }
            set { Session["OtherProductPrice"] = value; }
        }

      
        public UserMember LoginUserMember
        {
            get
            {
                if (Session["UserMember"] != null)
                {
                    return Session["UserMember"] as UserMember;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                Session["UserMember"] = value;
            }
        }


       public string SetProductPrice()
        {
            string priceMessage = string.Empty;
            if (DicProductPrice == null || DicProductPrice.Count <= 0)
            {
                priceMessage = "您没有选取商品！";
                return priceMessage;
            }
            if (DicProductPrice != null && LoginUserMember.DicUserCardList != null)
            {
                Dictionary<int,decimal> dicCards = new Dictionary<int,decimal>();
                foreach (dbCard oCard in LoginUserMember.DicUserCardList.Values)
                {
                    oProfessionAreaType = theProfessionAreaType.GetModelByCardTypeId(oCard.cardtypeId);
                    dicCards[oProfessionAreaType.Id] = dicCards.ContainsKey(oProfessionAreaType.Id) ? dicCards[oProfessionAreaType.Id] + oCard.cardprice : oCard.cardprice;
                }

                //if(ShopProductPrice != 0)
                //{
                //     priceMessage = "兑换商品与不能与商城商品同时提交，请选删除不同类商品！";
                //    return priceMessage;
                //}


                if (DicProductPrice.Count != dicCards.Count)
                {
                    priceMessage = "所选商品与卡券不能对应！";
                    return priceMessage;
                }
                foreach (int key in DicProductPrice.Keys)
                {
                    if (dicCards != null && dicCards[key] != DicProductPrice[key])
                    {
                        priceMessage = "所选商品与卡券不能对应！";
                        return priceMessage;
                    }
                }
               
                //if (DicProductPrice.ContainsKey(EnumProfessionPriceType.Card.ToString()) && DicProductPrice.Count == 1)
                //{
                //    return string.Empty;
                //}
                //else if (DicProductPrice.ContainsKey(EnumProfessionPriceType.Card.ToString()) && DicProductPrice.Count > 1)
                //{
                //    priceMessage = "如果要订购礼品卡，不能与商品一起提交！";
                //    return priceMessage;
                //}
                //if (DicProductPrice.ContainsKey(EnumProfessionPriceType.Shopping.ToString()) && LoginUserMember.AccountMoney < DicProductPrice[EnumProfessionPriceType.Shopping.ToString()])
                //{
                //    priceMessage = "账户礼品商城余额小于所选礼品商城余额！";
                //    return priceMessage;
                //}
                //else if (DicProductPrice.ContainsKey(EnumProfessionPriceType.Shopping.ToString()) && DicProductPrice.Count == 1 && DicProductPrice[EnumProfessionPriceType.Shopping.ToString()] < 100)
                //{
                //    priceMessage = "您需多付10元送货费，确认要购买吗？";
                //    return priceMessage;
                //}

                //else if (DicProductPrice.ContainsKey(EnumProfessionPriceType.Shopping.ToString()) && LoginUserMember.DicUserCardList == null && DicProductPrice.Count == 1
                //    || !DicProductPrice.ContainsKey(EnumProfessionPriceType.Shopping.ToString()) && LoginUserMember.DicUserCardList == null && DicProductPrice.Count == 0)
                //{
                //    return string.Empty;
                //}
                //else if (DicProductPrice.ContainsKey(EnumProfessionPriceType.Shopping.ToString()) && LoginUserMember.DicUserCardList != null && LoginUserMember.DicUserCardList.Count + 1 == DicProductPrice.Count
                //    || !DicProductPrice.ContainsKey(EnumProfessionPriceType.Shopping.ToString()) && LoginUserMember.DicUserCardList != null && LoginUserMember.DicUserCardList.Count == DicProductPrice.Count)
                //{
                    //foreach (string professKey in DicProductPrice.Keys)
                    //{
                    //    if (professKey == EnumProfessionPriceType.Shopping.ToString()) continue;
                    //    if (LoginUserMember.DicUserCardList == null || !LoginUserMember.DicUserCardList.ContainsKey(professKey))
                    //    {
                    //        priceMessage = "账户专区金额与所选专区类商品价格不一致！";
                    //        return priceMessage;
                    //    }
                    //    else
                    //    {
                    //        if (LoginUserMember.DicUserCardList[professKey] != null)
                    //        {
                    //            foreach (dbCard card in LoginUserMember.DicUserCardList[professKey])
                    //            {
                    //                dPrice = dPrice + card.cardprice;
                    //            }

                    //            if (dPrice != DicProductPrice[professKey])
                    //            {
                    //                priceMessage = "账户专区金额与所选专区类商品价格不一致！";
                    //                return priceMessage;
                    //            }
                              
                    //        }
                    //        else
                    //        {
                    //            priceMessage = "账户专区金额与所选专区类商品价格不一致！";
                    //            return priceMessage;
                    //        }
                    //    }
                    //}
                    //return string.Empty;

                //}
                //else
                //{
                //    priceMessage = "账户专区金额与所选专区类商品价格不一致！";
                //    return priceMessage;
                //}
            }

            return string.Empty;
        }

        
    }

    public class UserMember
    {
        string _memberId = string.Empty;

        public string MemberId
        {
            get { return _memberId; }
            set { _memberId = value; }
        }

        string _memberName = string.Empty;

        public string MemberName
        {
            get { return _memberName; }
            set { _memberName = value; }
        }

        bool _isCard = false;

        public bool IsCard
        {
            get { return _isCard; }
            set { _isCard = value; }
        }
        decimal _accountMoney;

        public decimal AccountMoney
        {
            get { return _accountMoney; }
            set { _accountMoney = value; }
        }

        public Dictionary<int, dbCard> DicUserCardList { get; set; }


    }

    
}